//
//  JCAPI.swift
//  JCNewsForMac
//
//  Created by ifly on 13/7/18.
//  Copyright © 2018年 Ismail. All rights reserved.
//

import Cocoa
import SwiftyJSON

//let JC_BASE_URL = "http://www.sdjcltd.com:60001/api/1.0/"
let JC_BASE_URL = "http://127.0.0.1:5000/api/1.0/"


class JCAPI: NSObject {
    
    /// 超级管理登录
    ///
    static func jcLogIn(_ userName:String,
                 password:String,
                 success:@escaping (_ returnData:[AnyHashable:Any])->Void,
                 failure:@escaping (_ errString:String,_ errCode:Int)->Void)->Void {
        
        let jcParame = [
            "UserName":userName,
            "PassWord":password
        ]

        JCNetWork.post(JC_BASE_URL + "adminLogin", parameters: jcParame, successs: { (jsonData) in
            
            let jcJson = JSON.init(jsonData)
            if jcJson["Code"].intValue == 200{
                success(jcJson["CodeData"].dictionaryObject!)
            }else{
                failure(jcJson["CodeString"].stringValue,jcJson["Code"].intValue)
            }
            
        }) { (error) in
            failure(error.localizedDescription,500)
        }
        
        
    }
    
    
    
    /// 获取所有企业
    ///
    /// - Parameters:
    ///   - page: 页码 默认1
    ///   - type: 企业类型 1.供应商 2.分销商
    ///   - state: 认证状态
    static func jcGetCompCerList(_ page:Int,type:Int, state:Int, success:@escaping (_ returnData:Array<Any>)->Void,
                          failure:@escaping (_ errString:String,_ errCode:Int)->Void) -> Void {

        let jcParameters = ["Token":JCUserModel.share().Token,
                            "Page":"\(page)",
                            "PageSize":"1000",
                            "Type":"\(type)",
                            "State":"\(state)"]
        
        JCNetWork.post(JC_BASE_URL + "getCerCompany", parameters: jcParameters, successs: { (jsonData) in
            let jcJson = JSON.init(jsonData)
            if jcJson["Code"].intValue == 200{
                success(jcJson["CodeData"].arrayObject!)
            }else{
                failure(jcJson["CodeString"].stringValue,jcJson["Code"].intValue)
            }
        }) { (error) in
            failure(error.localizedDescription,500)
        }

    }
    
    
    /// 获取所有宣传单
    ///
    /// - Parameters:
    ///   - page: 页码
    static func jcGetContentList(_ page:Int, pageSize:Int,Contentlabel:String,HotType:Int,success:@escaping (_ returnData:Array<Any>)->Void,
                                 failure:@escaping (_ errString:String,_ errCode:Int)->Void){
        
        let jcParameters = ["Token":JCUserModel.share().Token,
            "Page":"\(page)",
            "PageSize":"200",
            "Contentlabel":Contentlabel,
            "HotType":"\(HotType)"]
        
        JCNetWork.post(JC_BASE_URL + "adminGetContList", parameters: jcParameters, successs: { (jsonData) in
            let jcJson = JSON.init(jsonData)
            if jcJson["Code"].intValue == 200{
                success(jcJson["CodeData"].arrayObject!)
            }else{
                failure(jcJson["CodeString"].stringValue,jcJson["Code"].intValue)
            }
        }) { (error) in
            failure(error.localizedDescription,500)
        }
        
    }
    
    
    static func jcSetContentState(_ contentid:String,
                                  contentState:Int,success:@escaping (_ returnData:Dictionary<String, Any>)->Void,
                                  failure:@escaping (_ errString:String,_ errCode:Int)->Void){
        
        
        let jcParameters = ["Token":JCUserModel.share().Token,
            "ContentId":contentid,
            "HotType":"\(contentState)"]
        
        JCNetWork.post(JC_BASE_URL + "editHomeContent", parameters: jcParameters, successs: { (jsonData) in
                
            let jcJson = JSON.init(jsonData)
                
            if jcJson["Code"].intValue == 200{
                success(jcJson["CodeData"].dictionaryValue)
            }else{
                failure(jcJson["CodeString"].stringValue,jcJson["Code"].intValue)
            }
            }) { (error) in
                failure(error.localizedDescription,500)
            }
        
    }
    

}
